package main.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.ModuleConfig;
import com.justep.cloud.boot.service.X5ContextService;
import main.common.config.*;

public class RouteAction implements Action<ModuleConfig, ModuleConfig> {

	@Autowired
	private MyDispatcher myDispatcher;
	
	@Override
	public ModuleConfig execute(ModuleConfig actionRequest) {
		ModuleConfig moduleConfig = new ModuleConfig();
		HttpServletRequest req = (HttpServletRequest) actionRequest.getProperties().get("req");
		HttpServletResponse res = (HttpServletResponse) actionRequest.getProperties().get("res");
		try {
			X5ContextService.setContextLocal(SpringContextUtils.getApplicationContext());
			myDispatcher.doDispatch(req, res);
		} catch (Exception ex) {
			myDispatcher.processException(req, res, ex, null);
		} finally {
			X5ContextService.removeContextLocal();
		}
		return moduleConfig;
	}
	
	@Override
	public String getActionName() {
		return "routeaction";
	}
}